import { getSystemMenu } from '@/api/common/index'
import { traversalMenu } from '@/utils/utils.js'

const systemPower = {
  state: {
    systemlist: [], // 系统列表
    menus: [], // 系统列表
    power: {},
    userName: ''
  },
  mutations: {
    updatasystemlist(state, data) {
      state.systemlist = data
    },
    updataPower(state, data) {
      state.power = data
    },
    updataMenus(state, data) {
      state.menus = data
    },
    updateUserName(state, data) {
      state.userName = data
    }
  },
  getters: {
    systemlist: state => state.systemlist,
    power: state => state.power,
    menus: state => state.menus,
    userName: state => state.userName
  },
  actions: {
    getMenuList({ commit }) {
      return new Promise((resolve, reject) => {
        getSystemMenu({
          systemId: 28
        }).then(response => {
          if (response.mobBaseRes.code === 100) {
            const obj = traversalMenu(response.mobBaseRes.datas || []),
              list = obj.url,
              power = obj.power
            commit('updatasystemlist', list)
            commit('updataPower', power)
            commit('updataMenus', response.mobBaseRes.datas)
            window.sessionStorage.power = JSON.stringify(power)
          }
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}

export default systemPower
